package com.itheima.reggie.Service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.Mapper.SetmealMapper;
import com.itheima.reggie.Pojo.Setmeal;
import com.itheima.reggie.Pojo.SetmealDish;
import com.itheima.reggie.Service.SetmealDishService;
import com.itheima.reggie.Service.SetmealService;
import com.itheima.reggie.dto.SetmealDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
                                implements SetmealService {


    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    public void save_dish(SetmealDto setmealDto) {
//      新增套餐,新增之后数据库就会生成套餐的id
        this.save(setmealDto);
//      获取生成的套餐的id
        Long id = setmealDto.getId();
//      该集合中没有设置套餐id的值，所以需要在该集合内给套餐id设置值
        List<SetmealDish> list = setmealDto.getSetmealDishes();
//      item就是list每次遍历出来的那个对象
        list.stream().map((item)->{
//          给集合list设置套餐id的值
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());

//      批量添加setmealDish对象
        setmealDishService.saveBatch(list);

    }
}
